package com.chen.wxchat.controller;

import com.chen.wxchat.entity.Article;
import com.chen.wxchat.entity.Notify;
import com.chen.wxchat.entity.Reply;
import com.chen.wxchat.entity.UserInfo;
import com.chen.wxchat.service.ArticleService;
import com.chen.wxchat.service.NotifyService;
import com.chen.wxchat.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/superadmin")
@ResponseBody  //@RectController相当于Controller + ResponseBody
public class NotifyController {

    @Autowired
    private NotifyService notifyService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserInfoService userInfoService;



    private static Logger log = LoggerFactory.getLogger(NotifyController.class);

    @RequestMapping(value = "/getNotifyList",method = RequestMethod.GET)
    public Map<String,Object> getNotifyList(String notifyId){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        List<Notify> notifyList = notifyService.getNotifyList(notifyId);
        if (notifyId==null||notifyId==""){
            modelMap.put("success",true);
            modelMap.put("list",notifyList);
        }else {
            modelMap.put("success",true);
            modelMap.put("notify",notifyList.get(0));
        }
        log.info(modelMap.toString());
        return modelMap;
    }

    @RequestMapping(value = "/addNotify", method = RequestMethod.POST)
    private Map<String,Object> addNotify(@RequestBody Notify notify){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        if (notify == null) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "后台程序报错");
        } else {
            modelMap.put("success", notifyService.addNotify(notify));
        }
        return modelMap;
    }

    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    private Map<String,Object> addArticle(@RequestBody Article article){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        if (article == null) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "后台程序报错");
        } else {
            UserInfo user = userInfoService.getUserInfoById(article.getUserId());
            article.setUserId(user.getId());
            modelMap.put("success", articleService.addArticle(article));
        }
        return modelMap;
    }

    @RequestMapping(value = "/modifyArticle", method = RequestMethod.POST)
    private Map<String,Object> modifyArticle(@RequestBody Article article){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        if (article == null) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "后台程序报错");
        } else {
            modelMap.put("success", articleService.updateArticle(article));
        }
        return modelMap;
    }

    @RequestMapping(value = "/getArticleList",method = RequestMethod.GET)
    public Map<String,Object> getArticleList(String articleId){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        List<Article> articleList = null;
        if (articleId==null||articleId==""){
            articleList = articleService.getArticleList("");
            modelMap.put("success",true);
            modelMap.put("list",articleList);
        }else {
            articleList = articleService.getArticleList(articleId);
            if (articleList.size()!=0){
                Article article = articleList.get(0);
                int readCount = article.getReadCount()+1;
                article.setReadCount(readCount);
                articleService.updateArticleReadCount(article);
                modelMap.put("success",true);
                modelMap.put("article",article);
            }

        }
        log.info(modelMap.toString());
        return modelMap;
    }

    @RequestMapping(value = "/getAllArticleList",method = RequestMethod.GET)
    public Map<String,Object> getAllArticleList(){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        List<Article> articleList = articleList = articleService.getAllArticleList();
        modelMap.put("success",true);
        modelMap.put("list",articleList);

        log.info(modelMap.toString());
        return modelMap;
    }


    @RequestMapping(value = "/replyMessage",method = RequestMethod.POST)
    public Map<String,Object> replyMessage(@RequestBody Reply reply){
        Map<String,Object> modelMap = new HashMap<String, Object>();
        modelMap.put("success",articleService.addReply(reply));
        log.info(modelMap.toString());
        return modelMap;
    }



}
